
import torch
import torch.nn as nn
from torch.autograd import Variable 

import numpy as np
import matplotlib.pyplot as plt


# Hyper Parameters
input_size = 1
output_size = 1
num_epochs = 600
learning_rate = 0.001

# Toy Dataset
x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168]], dtype = np.float32)

y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573]], dtype = np.float32)


# Linear Regression Model
class LinearRegression(nn.Module):
    def __init__(self, input_size, output_size):
        super(LinearRegression, self).__init__()
        self.linear = nn.Linear(input_size, output_size)

    def forward(self, x):
        out = self.linear(x)
        return out

    
model = LinearRegression(input_size, output_size)

# Loss and Optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

# Train the Model

for epoch in range(num_epochs):

    inputs = Variable(torch.from_numpy(x_train))
    targets = Variable(torch.from_numpy(y_train))

    optimizer.zero_grad()
    outputs = model(inputs)

    loss = criterion(outputs, targets)
    loss.backward()
    optimizer.step()

    if (epoch+1)%5 == 0:
        print(epoch, num_epochs, loss.data)

predicted = model(Variable(torch.from_numpy(x_train))).data.numpy()
plt.plot(x_train, y_train, 'ro', lable='original data')
plt.plot(x_train, predicted, label='Fitted data')
plt.legend()
plt.show()

print(model.state_dict())







print('success')